<?php
class B2b_BlogController extends Zend_Controller_Action {

   /**
    * List blog of enterprise
    * @return void
    **/
    public function listAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
        	if($user->enterprise){
                $user_id = $user->user_id;
            }
            $this->view->assign('user', $user);
        }
		/**
		*	Assign PerPage
		*/
		$this->view->assign("perPage",10);
		
        $request = $this->getRequest();
        $lang = $request->getParam('lang');
        $this->view->assign('lang', $lang);

        $enterprise_id = $request->getParam('enterprise_id');

        /**
         * Check allow action
        **/
        $allowAction = ($enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();


        /*$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
        $permissionDao->setDbConnection($conn);
        $permission = $permissionDao->getPermission(@$user->enterprise->id);

        if($user->permission->blog_permission == 1){

        }*/


        /**
         * Blog models access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);
        //$blogDao->setLang($lang);
        $blogDao->setLang('vi_VN');

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Partner access to data
        **/
        $partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule("bizprofile")->getPartnerDao();
        $partnerDao->setDbConnection($conn);

        /**
         * Comment models access to db
         */
        $commentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getCommentDao();
        $commentDao->setDbConnection($conn);
        
        /** Khanh *****************************************************************************************************
	    * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
	    * $user_entity = 1 : User chua login
	    * $user_entity = 2 : User da login, nhung ko la DN
	    * $user_entity = 3 : User da login, la DN khach
	    * $user_entity = 4 : User da login, la DN doi tac
	    **/
        
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
		$permissionDao->setDbConnection($conn);
        //$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) 
		{ 
		    $user = $auth->getIdentity();
		    if(!isset($user->enterprise->id))
		    {
		      	$user_entity = 2; // user da login, nhung ko phai la DN
		    }
			else
			{
			   	$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		        $partnerDao->setDbConnection($conn);
		
		        $pageIndex     = 1;
		        $perPage = 5;
		        $offset     = ($pageIndex - 1) * $perPage;
		        $exp = array(
		            'relationid' => $params['enterprise_id'],
		            'partner_type' => 'buddy',
		            'partner_status' => 'yes',
					'deleted'		 => 0
		        );
						
		        //* In ralationship
		        $partners = $partnerDao->find($offset, $perPage, $exp);
		        $numPartners   = $partnerDao->count($exp);
		             
		        $user_entity = 3;// DN khach
		        $request = $this->getRequest();
		        foreach($partners as $row):
					$expRecomment = array(
					'relationid' => $row->relationid,
					'enterprise_id' => $row->enterprise_id, // DN doi tac
					);
					if($user->enterprise->id == $row->enterprise_id)
					{
						$user_entity = 4; // DN doi tac
					}
				endforeach;
			}
		}
		else 
		{
		   	$user_entity = 1; // user chua login 
		}
			
		$this->view->assign('user_entity', $user_entity);
			
		$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
		if($permission != null){
			$permission->enterprise_info = unserialize($permission->enterprise_info);			
			$this->view->assign('permission', $permission);
		}
    	if ($auth->hasIdentity()) { 
	            $user = $auth->getIdentity();
				if($user->enterprise){
					if($user->enterprise->id == $enterprise_id) // user đã login thuộc DN đang xem
			    	{
			    		$permission_viewall = 1;
			    		$this->view->assign('permission_viewall', $permission_viewall);
			    	}
				}
        	}
		/**
		* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		*****************************************************************************************************/

        /**
        * Condition chain
        **/
        $pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = 20;
        $offset    = ($pageIndex - 1) * $perPage;
        $where = "b.enterprise_id = $enterprise_id";

        /**
        * Get partner
        **/
        $partners = $partnerDao->listPartner($enterprise_id);
        $arrPartner = array();
        if(count($partners) > 0){
            foreach($partners as $partner){
                $where .= " OR b.enterprise_id = {$partner->id}";
            }
        }
        /**
         * Partner list
         **/
        /*$arrPartner = array(1,2);
        foreach($arrPartner as $partner){
            $where .= " OR b.enterprise_id = {$partner}";
        }*/

        $exp = array('enterprise' => $where);
        $blogs = $blogDao->find($offset, $perPage, $exp);
        $total = $blogDao->count($exp);

        /**
         * Get current enterprise
        **/
        $objEnterprise = $enterpriseDao->getById($enterprise_id);
        $found = true;
        if (null == $objEnterprise) {
            $found = false;
        }
        $this->view->assign('found', $found);
        $this->view->assign('enterprise', $objEnterprise);

        /**
         * Get blog en
        **/
        /*$en_blogs = null;
        if($lang == 'en_US'){
            $blogDao->setLang('en_US');
            $en_blogs = $blogDao->find($offset, $perPage, $exp);
        }*/

        /**
         * Get enterprise info follow blog
        **/
        $arrEnterprise = array();
        $arrBlogs = array();
        foreach($blogs as $key=>$blog){
            $enterprise = $enterpriseDao->getById($blog->enterprise_id);
            $arrEnterprise[$blog->blog_id] = $enterprise;

            /**
             * check if blog en exist
            **/
            $arrBlogs[$key] = $blog;
            if($lang == 'en_US'){
                $objBlog = $blogDao->getById($blog->blog_id, 'en_US');
                if($objBlog){
                    $arrBlogs[$key] = $objBlog;
                }
            }
        }
        $this->view->assign('enterprises', $arrEnterprise);

        /**
        * Paginator
        */
        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($blogs, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);

        $this->view->assign('exp', $exp);
        $this->view->assign('pageIndex', $pageIndex);

        $this->view->assign('blogs', $arrBlogs);
        $this->view->assign('numBlog', $total);

        $this->view->assign('paginator', $paginator);

        $arrRouteList = array(
            'enterprise_id' => $enterprise_id,
            'enterprise_name' => Tomato_Utility_String::removeSign($objEnterprise->company_name_en, '-', true)
        );
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url($arrRouteList, 'b2b_blog_list'),
            'itemLink' => 'page-%d',
        ));

        /**
         * Count comment foreach blog
        **/
        $arrComments = array();
        $arrUEComments = array();
        foreach($blogs as $blog){
            $exp['blog_id'] = $blog->blog_id;
            $comments = $commentDao->getCByBId($exp);
            $arrComments[$blog->blog_id] = $comments;

            /**
            * Get enterprise and user for comment
            **/
            $arrUserCInfo = array();
            foreach($comments as $comment){
                /**
                * enterprise account access to data
                **/
                $userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
                $userAccountDao->setDbConnection($conn);

                /**
                * User models access to data
                **/
                $userDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getUserDao();
                $userDao->setDbConnection($conn);

                /**
                * Set default value
                **/
                $arrSubUserCInfo = array('user' => null, 'enterprise' => null);

                /**
                * Get user info
                **/
                $userInfo = $userDao->getById($comment->user_id);
                $arrSubUserCInfo['user'] = $userInfo;
                $userAccount = $userAccountDao->getByUser($comment->user_id);
                if($userAccount){
                    $e_id = $userAccount->enterprise_id;

                    /**
                    * Get enterprise info for comment
                    **/
                    $ep_comment = $enterpriseDao->getById($e_id);
                    $arrSubUserCInfo['enterprise'] = $ep_comment;
                }
                $arrUserCInfo[$comment->id] = $arrSubUserCInfo;
            }
            $arrUEComments[$blog->blog_id] = $arrUserCInfo;
        }
        $this->view->assign('arrComments', $arrComments);
        $this->view->assign('arrUEComments', $arrUEComments);
    }

    /**
    * View blog detail of enterprise
    * Allow view blog follow language
    * @return void
    **/
    public function viewAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
		$redirectFlag = false;
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();			            	
            $this->view->assign('user', $user);
        }
		
		//$this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));		

        $request = $this->getRequest();
        $lang = $request->getParam('lang');
        $this->view->assign('lang', $lang);
        $blog_id = $request->getParam('blog_id');
        $flag = $request->getParam('comment');
        $this->view->assign('flag', $flag);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Blog models access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);
        $blogDao->setLang($lang);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Comment models access to db
         */
        $commentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getCommentDao();
        $commentDao->setDbConnection($conn);

        /**
         * Condition
        **/
        $lang_code = ($request->getParam('lang_code'))?$request->getParam('lang_code'):null;
        $this->view->assign('lang_code', $lang_code);

        /**
         * Get blog by lang and id
        **/
        $blog = $blogDao->getById($blog_id, $lang_code);
        if(null == $blog){
            $blog = $blogDao->getById($blog_id, 'vi_VN');
        }
        $found = true;
        if (null == $blog) {
            $found = false;
            $this->view->assign('found', $found);
            return;
        }
        $this->view->assign('found', $found);
        $this->view->assign('blog', $blog);

        /**
         * Check allow action
        **/
        $allowAction = ($blog->enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
         * Get language from system had beeen enanbled
        **/
        $file      = TOMATO_APP_DIR . DS . 'config' . DS . 'application.ini';
        $config    = new Zend_Config_Ini($file, null, array('allowModifications' => true));
        $config    = $config->toArray();
        $default   = isset($config['localization']['languages']['default'])
                        ? $config['localization']['languages']['default']
                        : $config['web']['lang'];
        $available = isset($config['localization']['languages']['list'])
                        ? explode(',', $config['localization']['languages']['list'])
                        : array($default);

        /**
         * Get all blog by id without lang
        **/
        $exp = array();
        $exp['blog_id'] = $blog_id;
        $blogs = $blogDao->getByIdNLang($exp);
        /**
        * Check blog content with other language
        **/
        $arrOtherBlogs = array();
        if(count($blogs) > 1){
            foreach($blogs as $r_blog){
                if($r_blog->language != $blog->language){
                    $arrOtherBlogs[] = $r_blog;
                }
            }
        }
        $this->view->assign('arrOtherBlogs', $arrOtherBlogs);

        /**
        * Check add more blog language content
        **/
        $allowAddMore = true;
        if(count($blogs) >= count($available)){
            $allowAddMore = false;
        }
        $this->view->assign('allowAddMore', $allowAddMore);

        /**
         * Get enterprise info follow blog
        **/
        $enterprise = $enterpriseDao->getById($blog->enterprise_id);
        $this->view->assign('enterprise', $enterprise);

        /**
         * Get comment for blog
        **/
        $exp['blog_id'] = $blog_id;
        $comments = $commentDao->getCByBId($exp);
        $this->view->assign('comments', $comments);
        $pageIndex  = 1;
		$perPage    = 10;
		$total      = count($comments);
		
		$this->view->assign("perPage",$perPage);
		$this->view->assign("pageIndex",$pageIndex);
		
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($comments, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
		$this->view->assign('paginator', $paginator);
		
        $this->view->assign('paginatorOptions', array(
            'path'        => "page",
            'itemLink' => '%d',
        ));
		
        /**
         * Get enterprise and user for comment
        **/
        $arrUserCInfo = array();
        foreach($comments as $comment){
            /**
            * enterprise account access to data
            **/
            $userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
            $userAccountDao->setDbConnection($conn);

            /**
             * User models access to data
             **/
            $userDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getUserDao();
            $userDao->setDbConnection($conn);

            /**
             * Set default value
            **/
            $arrSubUserCInfo = array('user' => null, 'enterprise' => null);

            /**
            * Get user info
            **/
            $userInfo = $userDao->getById($comment->user_id);
            $arrSubUserCInfo['user'] = $userInfo;
            $userAccount = $userAccountDao->getByUser($comment->user_id);
            if($userAccount){
                $e_id = $userAccount->enterprise_id;

                /**
                 * Get enterprise info for comment
                **/
                $ep_comment = $enterpriseDao->getById($e_id);
                $arrSubUserCInfo['enterprise'] = $ep_comment;
            }
            $arrUserCInfo[$comment->id] = $arrSubUserCInfo;
        }
        $this->view->assign('arrUserCInfo', $arrUserCInfo);
    }

    /**
    * Add blog of enterprise
    * Allow add blog follow language
    * @return void
    **/
    public function addAction(){

        //print_r($this->getRequest()->getParams());die;

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
			$redirectFlag = false;            			
            if($user->enterprise != null){
				$enterprise_id_edit = $this->getRequest()->getParam("enterprise_id");			
				if($user->enterprise->id != $enterprise_id_edit)
					$redirectFlag = true;
			}else $redirectFlag = true;		
			if($redirectFlag){
				$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
				$redirector->gotoUrl('');								
			}			
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $request = $this->getRequest();
        $blog_id = $request->getParam('id');
        $this->view->assign('blog_id', $blog_id);
        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Blog models access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);

        /**
        * Enterprise User model access to db
        */
        /*$userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
        $userAccountDao->setDbConnection($conn);*/

        /**
         * Enterprise basic model access to db
         */
        /*$enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);*/

        /**
         * Get relationship between user and enterprise
        **/
        /*$userAccount = $userAccountDao->getByUser($user->user_id);*/

        /**
         * Get enterprise info
        **/
        /*$enterpise = $enterpriseDao->getById($userAccount->enterprise_id);*/
        $this->view->assign('enterprise', $user->enterprise);

        /**
         * Get language from system had beeen enanbled
        **/
        $file      = TOMATO_APP_DIR . DS . 'config' . DS . 'application.ini';
        $config    = new Zend_Config_Ini($file, null, array('allowModifications' => true));
        $config    = $config->toArray();
        $default   = isset($config['localization']['languages']['default'])
                        ? $config['localization']['languages']['default']
                        : $config['web']['lang'];
        $available = isset($config['localization']['languages']['list'])
                        ? explode(',', $config['localization']['languages']['list'])
                        : array($default);

        if($blog_id){
            /**
            * Get all blog by id without lang
            **/
            $exp = array();
            $exp['blog_id'] = $blog_id;
            $blogs = $blogDao->getByIdNLang($exp);
            $this->view->assign('blogs', $blogs);

            /**
            * Check blog existed all language
            **/
            if(count($blogs) >= count($available)){
                $arrRouteView = array(
                    'enterprise_id' => $user->enterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true),
                    'blog_id' => $blog_id,
                    'slug' => null,
                );
                $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteView, 'b2b_blog_view'));
            }
        }

        if($request->isPost()){
            $blog_id       = $blog_id?$blog_id:null;
            $language      = $request->getPost('language');
            $title         = $request->getPost('title');
            $slug          = $request->getPost('slug');
            $link          = $request->getPost('link');
            $content       = $request->getPost('content');
            $date          = date('Y-m-d');
            $enterprise_id = @$user->enterprise->id;

            $blog = new B2b_Models_Blog(array(
                    'language'      => $language,
                    'title'         => $title,
                    'slug'          => $slug,
                    'link'          => $link,
                    'content'       => $content,
                    'date'          => $date,
                    'enterprise_id' => $enterprise_id,
            ));

            /**
             * Check condition isset blog_id
             * If isset blog_id is add to data with blog_id
             * Else use blog_id with auto increment
            **/
            ($blog_id)?$blog->blog_id = $blog_id:null;

            /**
             * Add to database
            **/
            $id = $blogDao->add($blog);
            if($id){
                /*$this->_helper->getHelper('FlashMessenger')
                ->addMessage($this->view->translator('b2b_blog_add_success'));*/

                $arrRouteView = array(
                    'enterprise_id' => $user->enterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true),
                    'blog_id' => $id,
                    'slug' => $slug,
                );
                $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteView, 'b2b_blog_view'));
            }
        }
    }

    /**
    * Edit blog of enterprise
    * Allow edit blog follow language
    * @return void
    **/
    public function editAction(){
        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
			$redirectFlag = false;
            if($user->enterprise != null){
				$enterprise_id_edit = $this->getRequest()->getParam("enterprise_id");			
				if($user->enterprise->id != $enterprise_id_edit)
					$redirectFlag = true;
			}else $redirectFlag = true;		
			if($redirectFlag){
				$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
				$redirector->gotoUrl('');								
			}			
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $request = $this->getRequest();
        $lang = $request->getParam('lang');
        $this->view->assign('lang', $lang);
        $blog_id = $request->getParam('blog_id');
        
        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Blog models access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);
        $blogDao->setLang($lang);

        /**
        * Enterprise User model access to db
        */
        /*$userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
        $userAccountDao->setDbConnection($conn);*/

        /**
         * Enterprise basic model access to db
         */
        /*$enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);*/

        /**
         * Get relationship between user and enterprise
        **/
        /*$userAccount = $userAccountDao->getByUser($user->user_id);*/

        /**
         * Get enterprise info
        **/
        /*$enterpise = $enterpriseDao->getById($userAccount->enterprise_id);*/
        $this->view->assign('enterprise', $user->enterprise);

        /**
         * Condition
        **/
        $lang_code = ($request->getParam('lang_code'))?$request->getParam('lang_code'):null;

        /**
         * Get blog by lang and id
        **/
        $blog = $blogDao->getById($blog_id, $lang_code);
        $found = true;
        if (null == $blog) {
            $found = false;
            $this->view->assign('found', $found);
            return;
        }
        $this->view->assign('found', $found);
        $this->view->assign('blog', $blog);

        /**
         * Check allow action
        **/
        $allowAction = ($blog->enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        if($request->isPost()){
            $blog_id       = $blog_id;
            $title         = $request->getPost('title');
            $slug          = $request->getPost('slug');
            $link          = $request->getPost('link');
            $content       = $request->getPost('content');

            $blog = new B2b_Models_Blog(array(
                    'blog_id'       => $blog_id,
                    'title'         => $title,
                    'slug'          => $slug,
                    'link'          => $link,
                    'content'       => $content,
                    'language'     => ($lang_code)?$lang_code:$lang,
            ));

            /**
             * Update to database
            **/
            
            $bool = $blogDao->update($blog);
            
            if($bool){
                $this->_helper->getHelper('FlashMessenger')
                ->addMessage($this->view->translator('b2b_blog_update_success'));

                /**
                * Param for url;
                **/
                $arrRouteEdit = array(
                    'enterprise_id' => $user->enterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true),
                    'blog_id' => $blog_id,
                    'slug' => $slug,
                );
                $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteEdit, 'b2b_blog_edit'));
                
            }
        }
    }

    /**
    * Delete blog of enterprise
    * @return void
    **/
    public function deleteAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $this->_helper->getHelper('layout')->disableLayout();
        $this->_helper->getHelper('viewRenderer')->setNoRender();

        $request = $this->getRequest();
        $blog_id = $request->getParam('blog_id');
        $enterprise_id = $request->getParam('enterprise_id');

        /**
         * Check allow action
        **/
        $allowAction = ($enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Blog models access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);

        $blogDao->delete($blog_id);

        /**
         * Redirect to list blog
        **/
        $arrRouteList = array(
            'enterprise_id' => $user->enterprise->id,
            'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true)
        );
        $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteList, 'b2b_blog_list'));
    }

    public function commentAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $this->_helper->getHelper('layout')->disableLayout();

        $request = $this->getRequest();
        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
		
        /**
         * Comment models access to db
         */
        $commentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getCommentDao();
        $commentDao->setDbConnection($conn);
		
		$recommentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		$recommentDao->setDbConnection($conn);
		
        if($request->isPost()){
            $user_id = $request->getPost('user_id');
            $blog_id = $request->getPost('blog_id');
            $content = $request->getPost('content');
			
            $date    = date('Y-m-d h:i:s');
            $enterprise_id = $request->getPost('enterprise_id');
            $this->view->assign('enterprise_id', $enterprise_id);	
            $comment = new B2b_Models_Comment(array(
                    'user_id' => $user_id,
                    'blog_id' => $blog_id,
                    'content' => $content,
                    'date'    => $date,
            ));
            $id = $commentDao->add($comment);
			
        }
		
        $comments = null;
        if($blog_id){
            /**
            * Get comment for blog
            **/
            $exp['blog_id'] = $blog_id;
            $comments = $commentDao->getCByBId($exp);
        }
		//print_r($comment);die;
        $this->view->assign('comments', $comments);

        /**
         * Get enterprise
        **/
        $arrUserCInfo = array();
        foreach($comments as $comment){

            /**
            * Enterprise basic model access to db
            */
            $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
            $enterpriseDao->setDbConnection($conn);

            /**
            * enterprise account access to data
            **/
            $userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
            $userAccountDao->setDbConnection($conn);

            /**
             * User models access to data
             **/
            $userDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getUserDao();
            $userDao->setDbConnection($conn);

            /**
             * Set default value
            **/
            $arrSubUserCInfo = array('user' => null, 'enterprise' => null);

            /**
            * Get user info
            **/
            $userInfo = $userDao->getById($comment->user_id);
            $arrSubUserCInfo['user'] = $userInfo;
            $userAccount = $userAccountDao->getByUser($comment->user_id);
            if($userAccount){
                $e_id = $userAccount->enterprise_id;

                /**
                 * Get enterprise info for comment
                **/
                $ep_comment = $enterpriseDao->getById($e_id);
                $arrSubUserCInfo['enterprise'] = $ep_comment;
            }
            $arrUserCInfo[$comment->id] = $arrSubUserCInfo;
        }
        $this->view->assign('arrUserCInfo', $arrUserCInfo);
		//print_r(arrUserCInfo);die;
    }

    public function commentDeleteAction(){
        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $this->_helper->getHelper('layout')->disableLayout();
        $this->_helper->getHelper('viewRenderer')->setNoRender();

        $request = $this->getRequest();
        $id = $request->getParam('id');
        $string = 'RESULT_ERROR';

        if ($request->isPost()) {
            /**
            * Get connection to database
            **/
            $conn = Tomato_Db_Connection::factory()->getMasterConnection();

            /**
            * Comment models access to db
            */
            $commentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getCommentDao();
            $commentDao->setDbConnection($conn);

            $bool = $commentDao->delete($id);
            if($bool){
                $string = 'RESULT_OK';
            }
        }
        $this->getResponse()->setBody($string);
    }
}